package utils;

import java.awt.Desktop;
import java.io.*;
import java.net.URI;

public class Openweb {
    private static final String TOMCAT_BIN_PATH = "D:/java_lib/tomcat/apache-tomcat-9.0.89/bin";
    public static void Open() {
        try {
            // 启动Tomcat
            System.out.println("尝试启动Tomcat...");
            ProcessBuilder start = new ProcessBuilder("cmd.exe", "/c",TOMCAT_BIN_PATH + "/startup.bat");
            start.directory(new File(TOMCAT_BIN_PATH));
            Process startProcess = start.start();
            // 等待一段时间以确保Tomcat完全启动
            Thread.sleep(2000); // 延长等待时间

            // 打开网页
            System.out.println("打开网页...");
            URI uri = new URI("http://localhost:8080/Dino/index.jsp");
            Desktop.getDesktop().browse(uri);

//            // 停止Tomcat
//            System.out.println("尝试停止Tomcat...");
//            ProcessBuilder stop = new ProcessBuilder("cmd.exe", "/c", TOMCAT_BIN_PATH + "/shutdown.bat");
//            stop.directory(new File(TOMCAT_BIN_PATH));
//            Process stopProcess = stop.start();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
